package com.zlsy.thread.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author zhouliang
 * @date 2020/7/1 20:36
 * <p>
 * 周期性执行的线程池
 */
@Slf4j
public class NewScheduledThreadPoolTest2 {

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        /**
         * 以固定的延时执行。run里面的线程占用时间会影响下次执行的时间。
         */
        scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            long start = System.currentTimeMillis();
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
                long end = System.currentTimeMillis();
                log.info("耗时：{}", end - start);
            }
        }, 1, 3, TimeUnit.SECONDS);
    }
}
